package repository

import (
	"dataaysn/internal/model"

	"gorm.io/gorm"
)

type HisDeptRepository interface {
	Create(dept *model.HisDept) error
	GetByID(id uint) (*model.HisDept, error)
	Update(dept *model.HisDept) error
	Delete(id uint) error
	ListByHospital(hospitalCode string) ([]model.HisDept, error)
	GetByDeptCode(deptCode string) (*model.HisDept, error)
}
type GormHisDeptRepository struct {
	db *gorm.DB
}

func NewGormHisDeptRepository(db *gorm.DB) HisDeptRepository {
	return &GormHisDeptRepository{db: db}
}

func (r *GormHisDeptRepository) Create(dept *model.HisDept) error {
	return r.db.Create(dept).Error
}

func (r *GormHisDeptRepository) GetByID(id uint) (*model.HisDept, error) {
	var dept model.HisDept
	err := r.db.First(&dept, id).Error
	return &dept, err
}

func (r *GormHisDeptRepository) Update(dept *model.HisDept) error {
	return r.db.Save(dept).Error
}

func (r *GormHisDeptRepository) Delete(id uint) error {
	return r.db.Delete(&model.HisDept{}, id).Error
}

func (r *GormHisDeptRepository) ListByHospital(hospitalCode string) ([]model.HisDept, error) {
	var depts []model.HisDept
	err := r.db.Where("hospital_code = ?", hospitalCode).Find(&depts).Error
	return depts, err
}

func (r *GormHisDeptRepository) GetByDeptCode(deptCode string) (*model.HisDept, error) {
	var dept model.HisDept
	err := r.db.Where("dept_code = ?", deptCode).First(&dept).Error
	return &dept, err
}
